﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using Lswweb.Domain;
using Lswweb.Model;

namespace Lswweb.Controls
{
    public class TreeCallBackDropDownList : CallbackDropDownList
    {
        public TreeCallBackDropDownList()
        {
            base.DataSourceEventHandler += new System.Web.UI.WebControls.ObjectDataSourceObjectEventHandler(TreeCallBackDropDownList_DataSourceEventHandler);
        }

        void TreeCallBackDropDownList_DataSourceEventHandler(object sender, System.Web.UI.WebControls.ObjectDataSourceEventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            if (ddl == null)
                return;

            int id = (e != null && e.ObjectInstance != null) ? Convert.ToInt32(e.ObjectInstance) : 0;
            if (id == 0 && BaseNodeId > 0)
            {
                if (showBaseNode)
                {
                    CategoryInfo info = TreeTables.TreeTable(TreeType).GetModelById(BaseNodeId);
                    if (info != null)
                        TreeTables.TreeTable(TreeType).BinderToListControl(ddl, new List<CategoryInfo>() { info });
                }
                else
                {
                    TreeTables.TreeTable(TreeType).BinderToListControl(ddl, BaseNodeId);
                }
                
            }
            else
            {
                TreeTables.TreeTable(TreeType).BinderToListControl(ddl, id);
            }
        }


        private string GetPath()
        {
            string selectdPath = string.Empty;
            if (selectValue > 0 && string.IsNullOrEmpty(selectdPath))
            {
                TreeTables treeTable = TreeTables.TreeTable(TreeType);

                IList<string> paths = treeTable.GetPath(selectValue).Split('/').ToList();
                int beginIndex = 0;
                if (BaseNodeId != 0)
                {
                    beginIndex=paths.IndexOf(BaseNodeId.ToString());
                    if(!ShowBaseNode)
                        beginIndex += 1;
                }

                for (int i = beginIndex; i < paths.Count; i++)
                    selectdPath += paths[i] + "/";

                selectdPath = selectdPath.TrimEnd('/');
            }
            return selectdPath;
        }

        public TreeTableType TreeType { get; set; }

        public new string SelectPath
        {
            get {return base.SelectPath; }
        }

        private int baseNode = 0;
        public int BaseNodeId
        {
            get { return baseNode; }
            set { baseNode = value; }
        }

        private bool showBaseNode = false;
        public bool ShowBaseNode
        {
            get { return baseNode > 0 ? showBaseNode : false; }
            set { showBaseNode = value; }
        }

        private int selectValue = 0;
        public new int SelectValue
        {
            get
            {
                string strValue = base.SelectValue;
                return string.IsNullOrEmpty(strValue) ? -1 : Convert.ToInt32(base.SelectValue);
            }
            set
            {
                selectValue = value;
                base.SelectPath = GetPath();
            }
        }
    }
}
